1 /***
2 * BytecodeLayout
3 *
4 * This class is designed to determine the
5 * proper order of the BlockVertex.
6 */
7
8 package junit.quilt.cover.generic;
9
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.HashMap;
13 import java.util.HashSet;
14
15 import junit.quilt.exception.QuiltException;
16 import junit.quilt.exception.InvalidTransitionException;
17
18 import org.apache.commons.graph.*;
19 import org.apache.commons.graph.algorithm.search.*;
20
21 import org.apache.bcel.classfile.*;
22 import org.apache.bcel.generic.*;
23
24 public class BytecodeLayout
25 implements org.apache.commons.graph.algorithm.search.Visitor
26 {
27 private Map startHandle = new HashMap(); // BV X START HANDLE
28 private Map endHandle = new HashMap(); // BV X END HANDLE
29
30 private MethodGen method = null;
31 private DirectedGraph graph = null;
32 private InstructionList il = new InstructionList();
33
34 private InvalidTransitionException err = null;
35 private boolean isOK = false;
36
37 public BytecodeLayout( MethodGen method ) {
38 this.method = method;
39 method.removeExceptionHandlers();
40 }
41
42 public void discoverGraph( Graph g ) {
43 this.graph = (DirectedGraph) g;
44 }
45
46 public void discoverVertex(Vertex v) {
47 BlockVertex bv = (BlockVertex) v;
48
49 InstructionHandle blockStart =
50 il.append( bv.copyInstructionList() );
51 InstructionHandle blockEnd =
52 il.getEnd();
53
54 startHandle.put( bv, blockStart );
55 endHandle.put( bv, blockEnd );
56 }
57
58 public void discoverEdge( Edge e ) {
59 }
60
61
62 public void finishEdge( Edge e ) {
63 FlowControlEdge fce = (FlowControlEdge) e;
64
65 BlockVertex source = (BlockVertex) graph.getSource( fce );
66 BlockVertex target = (BlockVertex) graph.getTarget( fce );
67
68 try {
69 fce.connect( method,
70 il,
71 (InstructionHandle) endHandle.get( source ),
72 (InstructionHandle) startHandle.get( target ) );
73 } catch (InvalidTransitionException ex) {
74 System.err.println("Warning: Instrumenation not completed.");
75 ex.printStackTrace();
76 this.err = ex;
77 }
78 }
79
80 public void finishVertex( Vertex v ) { }
81 public void finishGraph( Graph g ) {
82 isOK = true;
83 }
84
85 public void correctLocals( MethodGen method ) {
86 LocalVariableGen lvg[] = method.getLocalVariables();
87 // Map indexLVG = new HashMap(); // INDEX X LVG
88
89 InstructionList il = method.getInstructionList();
90 // InstructionHandle curr = il.getStart();
91 // Set visitedLVG = new HashSet();
92
93 for (int i = 0; i < lvg.length; i++) {
94 lvg[i].setStart( il.getStart() );
95 lvg[i].setEnd( il.getEnd() );
96 }
97
98 // for (int i = 0; i < lvg.length; i++) {
99 // indexLVG.put( new Integer( lvg[i].getIndex() ),
100 // lvg[i] );
101 // }
102
103 // while (curr.getNext() != null) {
104 // if (curr.getInstruction() instanceof LocalVariableInstruction) {
105 // LocalVariableInstruction lvi =
106 // (LocalVariableInstruction) curr.getInstruction();
107
108 // LocalVariableGen currGen =
109 // (LocalVariableGen) indexLVG.get(new Integer(lvi.getIndex()));
110
111 // if (currGen != null) {
112 // if (!visitedLVG.contains( currGen )) {
113 // visitedLVG.add( currGen );
114
115 // currGen.setStart( curr );
116 // }
117 // currGen.setEnd( curr );
118 // }
119 // }
120 // curr = curr.getNext();
121 // }
122 }
123
124 public Method getMethod()
125 throws QuiltException
126 {
127 if (err != null)
128 throw err;
129
130 if (!isOK)
131 throw new QuiltException("Method Not Ready.");
132
133 method.setInstructionList( il );
134 method.removeNOPs();
135
136 correctLocals( method );
137
138 method.setMaxStack();
139 method.setMaxLocals();
140 method.stripAttributes( true );
141 return method.getMethod();
142 }
143 }
144
145
146
This page was automatically generated by Maven